﻿using System;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;

namespace AMM.UserControls {
    public partial class CustomerEditControl : BaseEditControl {

        private FlexBindingSource<Customer> _fbs;

        public override object BaseEditModel {
            get { return this._fbs.CurrentItem; }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<Customer>(); }
                this._fbs.CurrentItem = (Customer)value;
            }
        }

        public CustomerEditControl() {
            InitializeComponent();
        }

        private void CustomerEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<Customer>(); }
            this.CreateFormBindings();
            this.BindValidation();
        }               
        
        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.tbName, x => x.Text, y => y.Name);
            this._fbs.CreateBinding(this.tbAddress, x => x.Text, y => y.Address);
            this._fbs.CreateBinding(this.tbCountry, x => x.Text, y => y.Country);
            this._fbs.CreateBinding(this.tbZipcode, x => x.Text, y => y.Zipcode);
            this._fbs.CreateBinding(this.tbLinkman1, x => x.Text, y => y.Linkman1);
            this._fbs.CreateBinding(this.tbPhone1, x => x.Text, y => y.Phone1);
            this._fbs.CreateBinding(this.tbLinkman2, x => x.Text, y => y.Linkman2);
            this._fbs.CreateBinding(this.tbPhone2, x => x.Text, y => y.Phone2);
            this._fbs.CreateBinding(this.tbFax, x => x.Text, y => y.Fax);
            this._fbs.CreateBinding(this.tbEmail, x => x.Text, y => y.Email);
            this._fbs.CreateBinding(this.tbWebsite, x => x.Text, y => y.Website);
            this._fbs.CreateBinding(this.cbIsEnabled, x => x.Checked, y => y.IsEnabled);
        }
        
        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.tbName, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbName, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbAddress, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbAddress, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbLinkman1, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbLinkman1, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbPhone1, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbPhone1, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbPhone1, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPhone1, "编码只能由'-'和数字组成");

            this.ValidatorForEdit.SetType(this.tbPhone2, ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRegularExpression(this.tbPhone2, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbPhone2, "编码只能由'-'和数字组成");
        }
    }
}
